/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.user.personal;

import hibernate.entity.Accounts;
import hibernate.entity.Staffs;
import hibernate.entity.Students;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import ui.common.AbstractUIBean;
import util.Md5Generator;

/**
 *
 * @author Neo
 */
@ManagedBean
@SessionScoped
public class RegisterBean extends AbstractUIBean {

    /**
     * Creates a new instance of RegisterBean
     */
    public RegisterBean() {
    }
    int roleID;
    Accounts currAcc = new Accounts();
    String confirmPassword;

    public Accounts getCurrAcc() {
        return currAcc;
    }

    public void setCurrAcc(Accounts currAcc) {
        this.currAcc = currAcc;
    }



    public int getRoleID() {
        return roleID;
    }

    public void setRoleID(int roleID) {
        this.roleID = roleID;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String doregister() {
        try {
            hbTransaction = hbSession.beginTransaction();
            hbSession.save(currAcc);
            currAcc.setRoleId(roleID);
            currAcc.setPassword("staff");
            currAcc.setPassword(Md5Generator.getMd5(currAcc.getPassword()));
            switch (roleID) {
                case 2:
                    Staffs s = new Staffs();
                    s.setStatus(0);
                    s.setAccounts(currAcc);
                    hbSession.save(s);
                    break;
                case 3:
                    Students st = new Students();
                    st.setStatus(0);
                    st.setAccounts(currAcc);
                    hbSession.save(st);

                    break;
            }
            hbSession.update(currAcc);
            hbTransaction.commit();
            return "success_register";
        } catch (Exception e) {
            hbTransaction.commit();
            return null;
        }

    }
}
